﻿/*
=======================================================
    Message【消息】
=======================================================
*/

namespace Services
{
    public interface IMessageMapping : IMapping<Message, MessageModel, MessageFilterModel>
    { }

    public class MessageMapping : AutoMessageMapping, IMessageMapping
    {
        public MessageMapping(ICurrentUser currentUser)
            : base(currentUser)
        { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override MessageModel Entity2Model(Message entity)
        {
            if (entity == null)
            {
                return null;
            }

            MessageModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/



            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override Message Model2Entity(MessageModel model)
        {
            Message entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/



            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<MessageModel> Entity2Model(IEnumerable<Message> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<Message> Model2Entity(IEnumerable<MessageModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<Message> FilterModel2Where(MessageFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                where = where.And(x => x.ReceiveUserID == _currentUser.CurrentUserInfo.UserID);
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            ////标题
            //if (!string.IsNullOrEmpty(filterModel._Title))
            //{
            //    where = where.And(x => x.Title.Contains(filterModel._Title));
            //    //where = where.And(x => x.Title == filterModel._Title);
            //}

            ////接收用户
            //if (filterModel._ReceiveUserID.HasValue)
            //{
            //    where = where.And(x => x.ReceiveUserID == filterModel._ReceiveUserID.Value);
            //}

            ////数据
            //if (!string.IsNullOrEmpty(filterModel._Data))
            //{
            //    where = where.And(x => x.Data.Contains(filterModel._Data));
            //    //where = where.And(x => x.Data == filterModel._Data);
            //}


            return where;
        }
        #endregion
    }
}
